package com.atguigu.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.cache.ShopCache;
import com.atguigu.entity.BaseCategoryView;
import com.atguigu.mapper.BaseCategoryViewMapper;
import com.atguigu.service.BaseCategoryViewService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * VIEW 服务实现类
 * </p>
 *
 * @author yangchaolove
 * @since 2021-11-20
 */
@Service
public class BaseCategoryViewServiceImpl extends ServiceImpl<BaseCategoryViewMapper, BaseCategoryView> implements BaseCategoryViewService {

    @Override
    @ShopCache(prefix = "getIndexCategoryInfo")
    public List<JSONObject> getIndexCategoryInfo() {
        List<JSONObject> parentCategoryList = new ArrayList<>();
        //1.获取所有的分类数据信息
        List<BaseCategoryView> categoryViewList = baseMapper.selectList(null);
        //2.根据category1Id进行分类
        Map<Long, List<BaseCategoryView>> category1Map = categoryViewList.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory1Id));
        int index = 1;
        for (Map.Entry<Long, List<BaseCategoryView>> category1Entry : category1Map.entrySet()) {
            Long category1Id = category1Entry.getKey();
            List<BaseCategoryView> category1List = category1Entry.getValue();
            String category1Name = category1List.get(0).getCategory1Name();
            //声明一个JSONObject 将一级分类数据存储上
            JSONObject category1 = new JSONObject();
            //category1.put("index",index);
            category1.put("categoryName",category1Name);
            category1.put("categoryId",category1Id);

            index++;

            //声明一个集合来存储一级分类下所有的二级分类数据
            List<JSONObject> category1Children = new ArrayList<>();
            //2.根据category2Id进行分类
            Map<Long, List<BaseCategoryView>> category2Map = category1List.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
            //循环遍历category2Map 获取二级分类Id，二级分类名称
            for (Map.Entry<Long, List<BaseCategoryView>> category2Entry : category2Map.entrySet()) {
                Long category2Id = category2Entry.getKey();
                List<BaseCategoryView> category2List = category2Entry.getValue();
                String category2Name = category2List.get(0).getCategory2Name();
                JSONObject category2 = new JSONObject();
                category2.put("categoryName",category2Name);
                category2.put("categoryId",category2Id);
                //讲二级分类放到category2Child集合里面
                category1Children.add(category2);

                List<JSONObject> category2Children = new ArrayList<>();

                category2List.stream().forEach(category3View ->{
                    JSONObject category3 = new JSONObject();
                    // 获取三级分类Id
                    category3.put("categoryId",category3View.getCategory3Id());
                    // 获取到三级分类的名称
                    category3.put("categoryName",category3View.getCategory3Name());
                    // 将获取到的三级分类数Id 放入集合。
                    category2Children.add(category3);
                });
                // 将三级分类数据集合放入二级分类数据
                category2.put("categoryChild",category2Children);
            }
            // 将二级分类数据集合放入一级分类数据
            category1.put("categoryChild",category1Children);
            // 将所有的一级分类数据放入到Json 集合中
            parentCategoryList.add(category1);
        }
        // 返回Json 对象集合
        return parentCategoryList;
    }
}
